package com.coco.service.impl;

import com.coco.config.JwtProperties;
import com.coco.entity.UserInfo;
import com.coco.model.CollectionCondition;
import com.coco.model.CollectionStatus;
import com.coco.model.VideoModel;
import com.coco.proxy.ManagerFeign;
import com.coco.service.VideoService;
import com.coco.utils.CookieUtils;
import com.coco.utils.JwtUtils;
import com.fasterxml.jackson.core.sym.Name1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GHOSTLaycoo
 * @date 2020/4/21 - 17:32
 */
@EnableConfigurationProperties(JwtProperties.class)
@Service
public class VideoServiceImpl implements VideoService{

    @Autowired
    private ManagerFeign managerFeign;

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public int changCollectionStatus(CollectionStatus collectionStatus, HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            CollectionCondition collectionCondition = new CollectionCondition();
            collectionCondition.setUserId(infoFromToken.getId());
            collectionCondition.setVideoId(collectionStatus.getVideoId());
            ResponseEntity<VideoModel> videoById = managerFeign.getVideoById(collectionStatus.getVideoId());
            int star = videoById.getBody().getStar();
            if(collectionStatus.getStatus()==1){
                ResponseEntity<Integer> integerResponseEntity = managerFeign.saveCollection(collectionCondition);
                managerFeign.updateStar(collectionStatus.getVideoId(), star+1);
                return integerResponseEntity.getBody();
            }else {
                ResponseEntity<Integer> integerResponseEntity = managerFeign.deleteCollection(collectionCondition);
                managerFeign.updateStar(collectionStatus.getVideoId(), star-1);
                return integerResponseEntity.getBody();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int isCollection(String videoId, HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            CollectionCondition collectionCondition = new CollectionCondition();
            collectionCondition.setUserId(infoFromToken.getId());
            collectionCondition.setVideoId(videoId);
            ResponseEntity<Integer> in = managerFeign.selectCollection(collectionCondition);
            if(in.getBody()>=1){
                return 1;
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<VideoModel> selectCollectionByUserId(HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            ResponseEntity<List<VideoModel>> listResponseEntity = managerFeign.selectCollectionByUserId(infoFromToken.getId());
            if(CollectionUtils.isEmpty(listResponseEntity.getBody())){
                return null;
            }
            return listResponseEntity.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
